package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.Constant;
import com.example.demo.common.SessionUtil;
import com.example.demo.mapper.ArticleMapper;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleInfoController {
    @Autowired
    private ArticleService articleService;
    @RequestMapping("/mylist")
    public List<ArticleInfo> getAllById(HttpServletRequest request){

        UserInfo userInfo= SessionUtil.getLoginUser(request);
        if(userInfo!=null){
            List<ArticleInfo> list=articleService.articleInfoList(userInfo.getId());
            return list;
        }
        return null;

    }

    @RequestMapping("/detail")
    public Object getDetail(Integer aid){

        if(aid>0&&aid!=null){
            return AjaxResult.success("查询成功",articleService.getDetails(aid));
        }
        return AjaxResult.fail("查询失败",-1);
    }

    @RequestMapping("/detailbyid")
    public Object getDetailById(HttpServletRequest request,Integer aid){
        UserInfo userInfo=SessionUtil.getLoginUser(request);
        ArticleInfo articleInfo=articleService.getDetails(aid);
        if(userInfo!=null&&articleInfo!=null&&articleInfo.getUid()==userInfo.getId()){
            return AjaxResult.success("查询成功",articleInfo);
        }
        return AjaxResult.fail("查询失败",-1);
    }

    @RequestMapping("/update")
    public Object update(HttpServletRequest request,Integer aid,String title,String content){
        UserInfo userInfo=SessionUtil.getLoginUser(request);
        ArticleInfo articleInfo=articleService.getDetails(aid);
        if(userInfo!=null&&articleInfo!=null&&articleInfo.getUid()==userInfo.getId()){
            return articleService.update(aid,articleInfo.getUid(),title,content);
        }
        return -1;

    }

    @RequestMapping("/edit")
    public Object edit(HttpServletRequest request,String title,String content){
        UserInfo userInfo=SessionUtil.getLoginUser(request);
        //ArticleInfo articleInfo=new ArticleInfo();
        if(userInfo!=null){

            return articleService.insert(userInfo.getId(),title,content);
        }
        return -1;

    }

    @RequestMapping("/list")
    public List<ArticleInfo> getList(Integer pindex,Integer psize){
        if(pindex==null||psize==null){
            return null;
        }
        Integer offset=psize*(pindex-1);
        return articleService.getList(psize,offset);
    }
    @RequestMapping("/totalpage")
    public Integer getTotalPage(Integer psize){
        if(psize!=null){
            return (int)Math.ceil(articleService.getTotalPage()*1.0/psize);
        }
        return null;
    }

    @RequestMapping("/delart")
    public Integer deleteArtById(Integer id){
        int res=articleService.deleteArtById(id);
        if(res==1){

            return res;
        }
        return 0;
    }

}
